package com.hh.vietnameseproject.ui.forget

import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.transition.Slide
import android.view.Gravity
import android.view.Window
import android.widget.Toast
import com.hh.vietnameseproject.databinding.ActivityForgetBinding
import com.hh.vietnameseproject.logic.network.AllNetWork
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class ForgetActivity : AppCompatActivity() {
    lateinit var binding:ActivityForgetBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityForgetBinding.inflate(layoutInflater)
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        val slide = Slide()
        slide.slideEdge = Gravity.RIGHT
        window.enterTransition = slide
        window.exitTransition = slide
        setContentView(binding.root)


        updateCode()
        binding.forgetPhonecodeBtn.setOnClickListener {
            val phoneNum = binding.forgetPhone.text.toString().trim()
            val phonePwd = binding.forgetNewPwd.text.toString().trim()
            val forgetCode = binding.forgetCodeEt.text.toString().trim()

            if (phoneNum == null || phoneNum.equals("")) {
                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (phonePwd == null || phonePwd.equals("")) {
                Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (forgetCode == null || forgetCode.equals("")) {
                Toast.makeText(this, "请输入静态验证码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            binding.forgetPhonecodeBtn.isEnabled = false
            GlobalScope.launch {
                val body = AllNetWork.getPhoneCode(phoneNum, forgetCode)
                val jsonObject = JSONObject(body.string())
                withContext(Dispatchers.Main) {
                    binding.forgetReturnText.text = jsonObject.getString("message")
                }
                if(jsonObject.getInt("code")==1){
                    withContext(Dispatchers.Main){
                        binding.forgetPhonecodeBtn.isEnabled = false
                        binding.forgetPhonecodeBtn.text = "0s后重发"
                    }
                    var i = 61
                    repeat(60) {
                        i--
                        withContext(Dispatchers.Main) {
                            binding.forgetPhonecodeBtn.text = "${i}s后重发"
                        }
                        Thread.sleep(1000)
                    }
                    withContext(Dispatchers.Main) {
                        binding.forgetPhonecodeBtn.isEnabled = true
                        binding.forgetPhonecodeBtn.text = "获取验证码"
                    }

                }else{
                    updateCode()
                    withContext(Dispatchers.Main){
                        binding.forgetPhonecodeBtn.isEnabled = true
                    }
                }
            }
        }
        binding.forgetBtn.setOnClickListener {
            val phoneNum = binding.forgetPhone.text.toString().trim()
            val phonePwd = binding.forgetNewPwd.text.toString().trim()
            val forgetCode = binding.forgetPhoneCodeEt.text.toString().trim()

            if (phoneNum == null || phoneNum.equals("")) {
                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (phonePwd == null || phonePwd.equals("")) {
                Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (forgetCode == null || forgetCode.equals("")) {
                Toast.makeText(this, "请输入动态验证码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            GlobalScope.launch {
                val body = AllNetWork.forgetCode(phoneNum,forgetCode,phonePwd)
                val jsonObject = JSONObject(body.string())
                val code = jsonObject.getInt("code")
                withContext(Dispatchers.Main) {
                    if(code==1){
                        Toast.makeText(this@ForgetActivity,"${jsonObject.getString("message")}",Toast.LENGTH_SHORT).show()
                        onBackPressed()
                    }else{
                        binding.forgetReturnText.text = jsonObject.getString("message")
                    }

                }
            }
        }

        binding.forgetCodeImage.setOnClickListener {
            updateCode()
        }

        binding.forgetBack.setOnClickListener {
            onBackPressed()
        }
    }
    fun updateCode(){
        GlobalScope.launch {
            val body = AllNetWork.getCheckCode()
            withContext(Dispatchers.Main) {
                binding.forgetCodeImage.setImageBitmap(BitmapFactory.decodeStream(body.byteStream()))
            }
        }
    }
    override fun onBackPressed() {
        finishAfterTransition()
        super.onBackPressed()
    }
}